import { app, BrowserWindow, dialog, globalShortcut, Menu } from 'electron';
import fs from 'node:fs';

function restartApp() {
  app.relaunch();
  app.quit();
  process.exit(0);
}

export default function setMenu(mainWin?: BrowserWindow) {
  // 菜单模板设置
  let template: Electron.MenuItemConstructorOptions[] = [
    {
      label: '重启1',
      click() {
        restartApp();
      },
    },
    {
      label: '测试目录',
      submenu: [
        {
          label: '发送',
          click: () => {
            mainWin.webContents.send('readFile', 'Hello');
          },
        },
        {
          label: '弹窗',
          click() {
            dialog.showMessageBox({
              message: 'Hello',
              title: '消息框',
            });
          },
        },
        {
          label: '对话框',
          click() {
            dialog.showOpenDialog({
              message: 'Hello',
              title: '测试标题',
            });
          },
        },
        {
          label: '全局快捷键',
          click() {
            globalShortcut.register('CommandOrControl+-', () => {
              dialog.showMessageBox({
                message: 'Hello',
                title: '消息框',
              });
            });
          },
        },
        {
          label: '读取文件',
          click() {
            const data = fs.readFileSync(`G:/git/ax/README.md`, {
              encoding: 'utf-8',
            });
            console.log(data);
          },
        },
        {
          label: '系统API',
          submenu: [
            {
              label: '居中',
              click() {
                mainWin.center();
              },
            },
            {
              label: '获取位置',
              click() {
                const position = mainWin.getPosition();
                console.log(position);
              },
            },
            {
              label: '移动鼠标',
              click() {
                const position = mainWin.getPosition();
                console.log(position);
              },
            },
          ],
        },
      ],
    },
  ];

  // 加载菜单
  let m = Menu.buildFromTemplate(template);
  Menu.setApplicationMenu(m);
}
